package handling.login.handler;

import client.MapleCharacter;
import client.MapleCharacterUtil;
import client.MapleClient;
import client.Skill;
import client.SkillEntry;
import client.SkillFactory;
import client.inventory.Item;
import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType;
import constants.JobConstants;
import handling.SendPacketOpcode;
import handling.login.LoginInformationProvider;
import handling.login.LoginInformationProvider.JobType;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import server.MapleItemInformationProvider;
import server.ServerProperties;
import server.quest.MapleQuest;
import tools.FileoutputUtil;
import tools.data.LittleEndianAccessor;
import tools.data.MaplePacketLittleEndianWriter;
import tools.packet.LoginPacket;

public class CreateCharHandler {

    private static final Logger log = Logger.getLogger(CreateCharHandler.class);
    public static byte[] geth(LittleEndianAccessor slea, MapleClient c) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.CREATE_VERIFY.getValue());
        mplew.write(0x04);
        return mplew.getPacket();
    
    
    }
    
    public static void handlePacket(LittleEndianAccessor slea, MapleClient c) {
        if (!c.isGm()) {
            String SendTo = "开始创建角色";
            System.out.println(SendTo);
            FileoutputUtil.packetLog("log\\PacketLog.log", SendTo);
        }
        
        if (!c.isLoggedIn()) {
            if (ServerProperties.IsLvkejian()) {
                String SendTo = "\r\n时间：" + FileoutputUtil.CurrentReadable_Time() + "  对玩家进行掉线处理 ----05 ";
                FileoutputUtil.packetLog("log\\玩家进行掉线处理.log", SendTo);
            }
            System.err.println("there 42");
            c.getSession().close(true);
            return;
        }
        
        
        String 名字;
        JobType job;
        byte 性别;
        byte 皮肤;
        byte 发色;
        short db;
        int 脸型;
        int 发型;
        int 脸饰 = -1;
        int 耳朵 = -1;
        int 尾巴 = -1;
        int 上衣;
        int 披风 = -1;
        int 裤子 = -1;
        int 鞋子;
        int 武器;
        int 盾牌 = -1;
        
        名字 = slea.readMapleAsciiString();
        if (!MapleCharacterUtil.canCreateChar(名字, false)) {
            System.out.println("char name hack: " + 名字);
            return;
        }
        slea.skip(4);  //过滤掉4个字节
        slea.skip(4);
        

        int 职业 = slea.readInt();
        job = JobType.getByType(职业);
        if (job == null) {
            System.out.println("New job type found: " + 职业);
            return;
        }
        for (JobConstants.LoginJob j : JobConstants.LoginJob.values()) {
            if (j.getJobType() == 职业) {
                if (j.getFlag() != JobConstants.LoginJob.JobFlag.启用.getFlag()) {
                    System.out.println("job was tried to be created while not enabled");
                    return;
                }
            }
        }
        
        db = slea.readShort();
        性别 = slea.readByte();
        皮肤 = slea.readByte();
        发色 = slea.readByte();
        脸型 = slea.readInt();
        发型 = slea.readInt();
        
        if (job.脸饰) {
            脸饰 = slea.readInt();
        }
        if (job.耳朵) {
            耳朵 = slea.readInt();
        }
        if (job.尾巴) {
            尾巴 = slea.readInt();
        }
        /*
        if (job.帽子) {
            帽子 = slea.readInt(); 
        }*/
        
        
        上衣 = slea.readInt();
        
        
        if (job.裤子) {
            裤子 = slea.readInt();
        }
        
        if (job.披风) {
            披风 = slea.readInt();
        }
        鞋子 = slea.readInt();
        武器 = job.type != 15?slea.readInt():0;
        int 神之子男武器 = job.type == 15 ? slea.readInt() : 0;
        int 神之子女武器 = job.type == 15 ? slea.readInt() : 0;
        if (slea.available() >= 4) {
            盾牌 = slea.readInt();
        }
        String mm = 
                "\r\n名字: " + 名字 + 
                "\r\n职业: " + 职业 + 
                "\r\n性别: " + 性别 + 
                "\r\n皮肤: " + 皮肤 + 
                "\r\n发型: " + 发型 + 
                "\r\n发色: " + 发色 + 
                "\r\n脸型: " + 脸型 + 
                "\r\n脸饰: " + 脸饰 + 
                "\r\n耳朵: " + 耳朵 + 
                "\r\n尾巴: " + 尾巴 + 
                "\r\n上衣: " + 上衣 + 
                "\r\n裤子: " + 裤子 + 
                "\r\n鞋子: " + 鞋子 + 
                "\r\n武器: " + 武器 + 
                "\r\n盾牌: " + 盾牌 + 
                "\r\n披风：" + 披风;
        System.out.println(mm);
        
        if (ServerProperties.IsLvkejian()) {
            FileoutputUtil.packetLog("log\\新建职业数据.log", mm);
        }

        MapleCharacter newchar = MapleCharacter.getDefault(c, job);
        newchar.setWorld((byte) c.getWorld());
        newchar.setFace(脸型);
        newchar.setHair(发型);
        newchar.setGender(性别);
        newchar.setName(名字);
        newchar.setSkinColor(皮肤);
        if (脸饰 < 0) {
            脸饰 = 0;
        }
        newchar.setFaceMarking(脸饰);
        int[] wrongEars = {1004062,1004063,1004064};
        int[] correctEars = {5010116, 5010117, 5010118};
        int[] wrongTails = {1102661, 1102662,1102663};
        int[] correctTails = {5010119, 5010120, 5010121};
        for (int i=0;i < wrongEars.length;i++) {
            if (耳朵 == wrongEars[i]) {
                耳朵 = correctEars[i];
            }
        }
        
        if (耳朵 < 0) {
            耳朵 = 0;
        }
        newchar.setEars(耳朵);
        
        for (int i=0;i < wrongTails.length;i++) {
            if (尾巴 == wrongTails[i]) {
                尾巴 = correctTails[i];
            }
        }
        
        if(尾巴 < 0) {
            尾巴 = 0;
        }
        newchar.setTail(尾巴);

        MapleItemInformationProvider li = MapleItemInformationProvider.getInstance();
        MapleInventory equip = newchar.getInventory(MapleInventoryType.EQUIPPED);

        Item item = li.getEquipById(上衣);
        item.setPosition((short) -5);
        equip.addFromDB(item);

        if (裤子 > 0) {
            item = li.getEquipById(裤子);
            item.setPosition((short) -6);
            equip.addFromDB(item);
        }

        item = li.getEquipById(鞋子);
        item.setPosition((short) -7);
        equip.addFromDB(item);

        if (((职业 == 7) || (职业 == 2) || (职业 == 10)) && (披风 > 0)) {
            item = li.getEquipById(披风);
            item.setPosition((short) -9);
            equip.addFromDB(item);
        }

        if (盾牌 > 0) {
            item = li.getEquipById(盾牌);
            item.setPosition((short) -10);
            equip.addFromDB(item);
        }

        switch (job) {
            case 爆莉萌天使:
                item = li.getEquipById(1352601);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 夜光法师:
                item = li.getEquipById(1352400);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 狂龙战士:
                item = li.getEquipById(1352500);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 双弩精灵:
                item = li.getEquipById(1352000);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 幻影:
                item = li.getEquipById(1352100);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 尖兵:
                item = li.getEquipById(1353001);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 神之子:
                item = li.getEquipById(1032202);
                item.setPosition((short) -4);
                equip.addFromDB(item);
                item = li.getEquipById(1082521);
                item.setPosition((short) -8);
                equip.addFromDB(item);
                item = li.getEquipById(1113059);
                item.setPosition((short) -12);
                equip.addFromDB(item);
                item = li.getEquipById(1122260);
                item.setPosition((short) -17);
                equip.addFromDB(item);
                item = li.getEquipById(1142634);
                item.setPosition((short) -26);
                equip.addFromDB(item);
                item = li.getEquipById(1132231);
                item.setPosition((short) -29);
                equip.addFromDB(item);
                item = li.getEquipById(1152137);
                item.setPosition((short) -30);
                equip.addFromDB(item);
        }

        if (武器 > 0) {
            item = li.getEquipById(武器);
            item.setPosition((short) -11);
            equip.addFromDB(item);
        }

        if (神之子女武器 > 0) {
            item = li.getEquipById(神之子女武器);
            item.setPosition((short) -10);
            equip.addFromDB(item);
        }

        if (神之子男武器 > 0) {
            item = li.getEquipById(神之子男武器);
            item.setPosition((short) -11);
            equip.addFromDB(item);
        }
        System.out.println("\r\n装备已经写入数据库\r\n");

        newchar.getInventory(MapleInventoryType.USE).addItem(new Item(2000013, (short) 0, (short) 100, (short) 0));
        newchar.getInventory(MapleInventoryType.USE).addItem(new Item(2000014, (short) 0, (short) 100, (short) 0));

        //新手指南
        int[][] guidebooks = new int[][]{{4161001, 0}, {4161047, 1}, {4161048, 2000}, {4161052, 2001}, {4161054, 3}, {4161079, 2002}};
        int guidebook = 0;
        for (int[] i : guidebooks) {
            if (newchar.getJob() == i[1]) {
                guidebook = i[0];
            } else if (newchar.getJob() / 1000 == i[1]) {
                guidebook = i[0];
            }
        }
        if (guidebook > 0) {
            newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(guidebook, (byte) 0, (short) 1, (byte) 0));
        }
        
        switch (job) {
            case 反抗者:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161001, (short) 0, (short) 1, (short) 0));
                break;
            case 冒险家:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161001, (short) 0, (short) 1, (short) 0));
                break;
            case 骑士团:
                newchar.setQuestAdd(MapleQuest.getInstance(20022), (byte) 1, "1");
                newchar.setQuestAdd(MapleQuest.getInstance(20010), (byte) 1, null);
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161047, (short) 0, (short) 1, (short) 0));
                break;
            case 战神:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161048, (short) 0, (short) 1, (short) 0));
                break;
            case 龙神:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161052, (short) 0, (short) 1, (short) 0));
                break;
            case 恶魔:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161079, (short) 0, (short) 1, (short) 0));
                break;
            case 双弩精灵:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161079, (short) 0, (short) 1, (short) 0));
            //case 尖兵:
            //case 6:
            //case 神之子:
        }
        if ((MapleCharacterUtil.canCreateChar(名字, c.isGm())) && ((!LoginInformationProvider.getInstance().isForbiddenName(名字)) || (c.isGm())) && ((c.isGm()) || (c.canMakeCharacter(c.getWorld())))) {
            System.out.println("准备保存新人物数据");
            MapleCharacter.saveNewCharToDB(newchar, job, db);
            c.getSession().write(LoginPacket.addNewCharEntry(newchar, true));
            c.createdChar(newchar.getId());
        } else {
            c.getSession().write(LoginPacket.addNewCharEntry(newchar, false));
        }
    }
}

/*
public static void handlePacket(LittleEndianAccessor slea, MapleClient c) {
        if (!c.isGm()) {
            String SendTo = "开始通过  CREATE_CHAR - CreateCharHandler -- handlePacket --- 01";
            System.out.println(SendTo);
            FileoutputUtil.packetLog("log\\PacketLog.log", SendTo);
        }
        
        if (!c.isLoggedIn()) {
            c.getSession().close(true);
            if (ServerProperties.IsLvkejian()) {
                String SendTo = "\r\n时间：" + FileoutputUtil.CurrentReadable_Time() + "  对玩家进行掉线处理 ----05 ";
                FileoutputUtil.packetLog("log\\玩家进行掉线处理.log", SendTo);
            }
            return;
        }
        
        String 名字 = slea.readMapleAsciiString();

        slea.skip(4);  //过滤掉4个字节
        slea.skip(4);

        int 职业类型 = slea.readInt();
        LoginInformationProvider.JobType jobType = LoginInformationProvider.JobType.getByType(职业类型);
        short db = slea.readShort();
        byte 性别 = slea.readByte();
        byte 皮肤颜色 = slea.readByte();
        byte 头发颜色 = slea.readByte();
        int 脸型 = slea.readInt();
        int 发型 = slea.readInt();

        int 脸饰 = (职业类型 == 16) || (职业类型 == 6) || (职业类型 == 14) ? slea.readInt() : 0;
        int 耳朵 = 职业类型 == 16 ? slea.readInt() : 0;
        int 尾巴 = 职业类型 == 16 ? slea.readInt() : 0;
        int 上衣 = slea.readInt();

        int 披风 = (职业类型 == 7) || (职业类型 == 2) || (职业类型 == 10) || (职业类型 == 15) ? slea.readInt() : 0;

        int 裤子 = (职业类型 == 16) || (职业类型 == 15) || (职业类型 == 1) || (职业类型 == 8) || (职业类型 == 13) || (职业类型 == 14) || (职业类型 == 2) || (职业类型 == 10) || (职业类型 == 0) || (职业类型 == 5) || (职业类型 == 6) || (职业类型 == 7) || (职业类型 == 11) || (职业类型 == 12) ? 0 : slea.readInt();

        int 鞋子 = slea.readInt();
        int 武器 = 职业类型 != 15 ? slea.readInt() : 0;

        int 神之子男武器 = 职业类型 == 15 ? slea.readInt() : 0;
        int 神之子女武器 = 职业类型 == 15 ? slea.readInt() : 0;


        int 盾牌 = 职业类型 == 6 ? slea.readInt() : 0;

        String mm = "\r\n名字: " + 名字 + "\r\n职业: " + 职业类型 + "\r\n性别: " + 性别 + "\r\n皮肤: " + 皮肤颜色 + "\r\n头发颜色: " + 头发颜色 + "\r\n脸型: " + 脸型 + "\r\n发型: " + 发型 + "\r\n脸饰: " + 脸饰 + "\r\n耳朵: " + 耳朵 + "\r\n尾巴: " + 尾巴 + "\r\n上衣: " + 上衣 + "\r\n裤子: " + 裤子 + "\r\n鞋子: " + 鞋子 + "\r\n武器: " + 武器 + "\r\n盾牌: " + 盾牌 + "\r\n披风：" + 披风;
        if (ServerProperties.ShowPacket()) {
            log.info(mm);
        }

        if (ServerProperties.IsLvkejian()) {
            FileoutputUtil.packetLog("log\\新建职业数据.log", mm);
        }

        MapleCharacter newchar = MapleCharacter.getDefault(c, jobType);
        newchar.setWorld((byte) c.getWorld());
        newchar.setFace(脸型);
        newchar.setHair(发型);
        newchar.setGender(性别);
        newchar.setName(名字);
        newchar.setSkinColor(皮肤颜色);
        newchar.setDecorate(脸饰);

        MapleItemInformationProvider li = MapleItemInformationProvider.getInstance();
        MapleInventory equip = newchar.getInventory(MapleInventoryType.EQUIPPED);

        Item item = li.getEquipById(上衣);
        item.setPosition((short) -5);
        equip.addFromDB(item);

        if (裤子 > 0) {
            item = li.getEquipById(裤子);
            item.setPosition((short) -6);
            equip.addFromDB(item);
        }

        item = li.getEquipById(鞋子);
        item.setPosition((short) -7);
        equip.addFromDB(item);

        if (((职业类型 == 7) || (职业类型 == 2) || (职业类型 == 10)) && (披风 > 0)) {
            item = li.getEquipById(披风);
            item.setPosition((short) -9);
            equip.addFromDB(item);
        }

        if (盾牌 > 0) {
            item = li.getEquipById(盾牌);
            item.setPosition((short) -10);
            equip.addFromDB(item);
        }

        switch (jobType) {
            case 爆莉萌天使:
                item = li.getEquipById(1352601);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 夜光法师:
                item = li.getEquipById(1352400);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 狂龙战士:
                item = li.getEquipById(1352500);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 双弩精灵:
                item = li.getEquipById(1352000);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 幻影:
                item = li.getEquipById(1352100);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 尖兵:
                item = li.getEquipById(1353001);
                item.setPosition((short) -10);
                equip.addFromDB(item);
                break;
            case 神之子:
                item = li.getEquipById(1032202);
                item.setPosition((short) -4);
                equip.addFromDB(item);
                item = li.getEquipById(1082521);
                item.setPosition((short) -8);
                equip.addFromDB(item);
                item = li.getEquipById(1113059);
                item.setPosition((short) -12);
                equip.addFromDB(item);
                item = li.getEquipById(1122260);
                item.setPosition((short) -17);
                equip.addFromDB(item);
                item = li.getEquipById(1142634);
                item.setPosition((short) -26);
                equip.addFromDB(item);
                item = li.getEquipById(1132231);
                item.setPosition((short) -29);
                equip.addFromDB(item);
                item = li.getEquipById(1152137);
                item.setPosition((short) -30);
                equip.addFromDB(item);
        }

        if (武器 > 0) {
            item = li.getEquipById(武器);
            item.setPosition((short) -11);
            equip.addFromDB(item);
        }

        if (神之子女武器 > 0) {
            item = li.getEquipById(神之子女武器);
            item.setPosition((short) -10);
            equip.addFromDB(item);
        }

        if (神之子男武器 > 0) {
            item = li.getEquipById(神之子男武器);
            item.setPosition((short) -11);
            equip.addFromDB(item);
        }

        newchar.getInventory(MapleInventoryType.USE).addItem(new Item(2000013, (short) 0, (short) 100, (short) 0));
        newchar.getInventory(MapleInventoryType.USE).addItem(new Item(2000014, (short) 0, (short) 100, (short) 0));

        switch (jobType) {
            case 反抗者:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161001, (short) 0, (short) 1, (short) 0));
                break;
            case 冒险家:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161001, (short) 0, (short) 1, (short) 0));
                break;
            case 骑士团:
                newchar.setQuestAdd(MapleQuest.getInstance(20022), (byte) 1, "1");
                newchar.setQuestAdd(MapleQuest.getInstance(20010), (byte) 1, null);
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161047, (short) 0, (short) 1, (short) 0));
                break;
            case 战神:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161048, (short) 0, (short) 1, (short) 0));
                break;
            case 龙神:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161052, (short) 0, (short) 1, (short) 0));
                break;
            case 恶魔猎手:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161079, (short) 0, (short) 1, (short) 0));
                break;
            case 双弩精灵:
                newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161079, (short) 0, (short) 1, (short) 0));
            //case 尖兵:
            //case 6:
            //case 神之子:
        }
        if ((MapleCharacterUtil.canCreateChar(名字, c.isGm())) && ((!LoginInformationProvider.getInstance().isForbiddenName(名字)) || (c.isGm())) && ((c.isGm()) || (c.canMakeCharacter(c.getWorld())))) {
            System.out.println("准备保存新人物数据");
            MapleCharacter.saveNewCharToDB(newchar, jobType, db);
            c.getSession().write(LoginPacket.addNewCharEntry(newchar, true));
            c.createdChar(newchar.getId());
        } else {
            c.getSession().write(LoginPacket.addNewCharEntry(newchar, false));
        }
    }
 */